//
//  SqDetailListViewController.m
//  FengyuanXS
//
//  Created by 王冬 on 2020/8/3.
//  Copyright © 2020 王冬. All rights reserved.
//   15287654321  处理中和已完成页面    15012345678  处理中和已完成页面

#import "SqDetailListViewController.h"
#import "SqFinshViewCell.h"
#import "TypeCell.h"
#import "BuZhouViewController.h"
@interface SqDetailListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *footView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIView *headView;
@property (weak, nonatomic) IBOutlet UIView *imagesView;


@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *shenqLabel;
@property (weak, nonatomic) IBOutlet UILabel *bumenLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;

@property (weak, nonatomic) IBOutlet UILabel *pingTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *pingContentLabel;
@property (weak, nonatomic) IBOutlet UILabel *pictorLabel;
@property (weak, nonatomic) IBOutlet UIButton *buZBtn;
- (IBAction)buZBtn:(UIButton *)sender;

@end

@implementation SqDetailListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"诉求详情";
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"SqFinshViewCell" bundle:nil] forCellReuseIdentifier:@"SqFinshViewCell"];
    
    self.nameLabel.text = [NSString stringWithFormat:@"%@提交的%@",self.model.appeal.real_name,self.model.appeal.type_name];
    self.shenqLabel.text = self.model.appeal.real_name;
    self.bumenLabel.text = self.model.appeal.section_name;
    self.timeLabel.text = [self ConvertStrToTime:self.model.appeal.create_time];
    self.typeLabel.text = self.model.appeal.type_name;
    self.contentLabel.text = self.model.appeal.content;
    self.pingTypeLabel.text = self.model.appraise.content;
    self.pingContentLabel.text = self.model.appraise.content;
    
    if (self.model.appraise) {
        self.footView.hidden = NO;
        
    }else{
        self.footView.hidden = YES;
    }
    
    if (self.model.appeal.more.photos.count >0) {
        self.pictorLabel.hidden = NO;
        self.imagesView.hidden = NO;
        self.headView.height = 290;
        [self.collectionView reloadData];
    }else{
        self.headView.height = 210;
    }
    
    
   
  
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    UICollectionViewFlowLayout *flow1 = [[UICollectionViewFlowLayout alloc] init];
       [flow1 setScrollDirection:UICollectionViewScrollDirectionHorizontal];//竖直滑动
       flow1.itemSize = CGSizeMake(70, 70);
       self.collectionView.delegate = self;
         flow1.sectionInset = UIEdgeInsetsMake(1.0, 1.0, 1.0, 1.0);
       self.collectionView.dataSource = self;
       self.collectionView.showsHorizontalScrollIndicator = NO;
       self.collectionView.showsVerticalScrollIndicator = NO;
       self.collectionView.collectionViewLayout = flow1;
       [self.collectionView registerNib:[UINib nibWithNibName:@"TypeCell" bundle:nil] forCellWithReuseIdentifier:@"TypeCell2"];//注册cel
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    //    return self.treeList.count;
    return self.model.appeal.more.photos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TypeCell2" forIndexPath:indexPath];
    PhotosModel *model = self.model.appeal.more.photos[indexPath.row];
    
    //    [cell.backImageView sd_setImageWithURL:[NSURL URLWithString:@"%@%@",BaseImgUrl,model.url]];
    
    NSString * str1 = @"\\";
    NSString  *imageUrl = [model.url stringByReplacingOccurrencesOfString:str1 withString:@"/"];
    [cell.backImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseImgUrl,imageUrl]]];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //      PhotosModel *model = self.model.more.photos[indexPath.row];
    [self selectedIndex:indexPath.row withImageURLs:self.model.appeal.more.photos];
}
#pragma mark - Table view data source

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    FlowModel *model = self.model.flow[indexPath.row];
    
    if (model.more.photos.count >0) {
        return 200;
    }else{
        return  115;
    }
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.model.flow.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SqFinshViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SqFinshViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    FlowModel *model = self.model.flow[indexPath.row];
    if (model.more.photos.count >0) {
        cell.imageLabel.hidden = NO;
        cell.selectImageView.hidden = NO;
    }else{
       cell.imageLabel.hidden = YES;
       cell.selectImageView.hidden = YES;
    }
    
     if ([model.auth isEqualToString:@"1"]) {
          cell.titleLabel.text = @"管理部门研判";
      }
      if ([model.auth isEqualToString:@"2"]) {
             cell.titleLabel.text = @"单位领导审批";
         }
      if ([model.auth isEqualToString:@"3"]) {
             cell.titleLabel.text = @"承办单位办理";
         }
      if ([model.auth isEqualToString:@"4"]) {
             cell.titleLabel.text = @"集团管理部门研判";
         }
      if ([model.auth isEqualToString:@"5"]) {
             cell.titleLabel.text = @"集团领导审批";
         }
      if ([model.auth isEqualToString:@"6"]) {
             cell.titleLabel.text = @"集团承办单位办理";
         }
    
    cell.bumenLabel.text = model.section_headoffice;
    cell.nameLabel.text = model.real_name;
    cell.timeLabel.text = [self ConvertStrToTime:model.create_time];
    cell.contentLabel.text = model.content;
    cell.imageArray = model.more.photos;
    WS(weakSelf);
    cell.selectItem = ^(NSInteger index) {
        [weakSelf selectedIndex:index withImageURLs: model.more.photos];
    };
    
    return cell;
}

- (void)selectedIndex:(NSInteger)index withImageURLs:(NSArray *)urlsArray{
    NSMutableArray *datas = [NSMutableArray array];
    
    for (PhotosModel *model in urlsArray) {
        YBIBImageData *data = [YBIBImageData new];
        NSString * str1 = @"\\";
        NSString  *imageUrl = [model.url stringByReplacingOccurrencesOfString:str1 withString:@"/"];
        data.imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseImgUrl,imageUrl]];
        [datas addObject:data];
    }
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = datas;
    browser.currentPage = index;
    [browser show];
}
//时间戳变为格式时间
- (NSString *)ConvertStrToTime:(NSString *)timeStr{
    long long time=[timeStr longLongValue];
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString*timeString=[formatter stringFromDate:date];
    return timeString;
    
}



- (IBAction)buZBtn:(UIButton *)sender {
    
    BuZhouViewController *bzVC = [[BuZhouViewController alloc]init];
    bzVC.appealID = self.model.appeal.id;
    [self.navigationController pushViewController:bzVC animated:YES];
    
}
@end
